package user

import (
	"BookStack/pkg/errors"
	appErrors "BookStack/pkg/errors"
)

// UserNotFound 用户不存在错误
func UserNotFound(userID interface{}) appErrors.AppError {
	return appErrors.New(appErrors.ErrCodeUserNotFound, "").WithDetails(map[string]interface{}{
		"user_id": userID,
	})
}

// UserAlreadyExists 用户已存在错误
func UserAlreadyExists(username string) appErrors.AppError {
	return appErrors.New(appErrors.ErrCodeUserAlreadyExists, "").WithDetails(map[string]interface{}{
		"username": username,
	})
}

// InvalidCredentials 用户名或密码错误
func InvalidCredentials() appErrors.AppError {
	return appErrors.New(appErrors.ErrCodeInvalidCredentials, "")
}

// AuthenticationFailed 认证失败
func AuthenticationFailed(reason string) appErrors.AppError {
	appErr := appErrors.New(appErrors.ErrCodeAuthentication, "")
	if reason != "" {
		appErr = appErr.WithDetails(map[string]interface{}{
			"reason": reason,
		})
	}
	return appErr
}

// PermissionDenied 权限不足
func PermissionDenied() appErrors.AppError {
	return appErrors.New(appErrors.ErrCodeAuthorization, "")
}

// ValidationFailed 验证失败
func ValidationFailed(fields map[string]string) appErrors.AppError {
	// 将字符串错误消息转换为错误码
	fieldErrors := make(map[string]int)
	for field, msg := range fields {
		switch {
		case field == "Username" && msg == "用户名不能为空":
			fieldErrors[field] = appErrors.ErrCodeUsernameRequired
		case field == "Email" && msg == "邮箱不能为空":
			fieldErrors[field] = appErrors.ErrCodeEmailRequired
		case field == "Password" && msg == "密码不能为空":
			fieldErrors[field] = appErrors.ErrCodePasswordRequired
		case field == "OldPassword" && msg == "旧密码不能为空":
			fieldErrors[field] = appErrors.ErrCodeOldPasswordRequired
		case field == "NewPassword" && msg == "新密码不能为空":
			fieldErrors[field] = appErrors.ErrCodeNewPasswordRequired
		case field == "Username" && msg == "用户名长度不能少于3个字符":
			fieldErrors[field] = appErrors.ErrCodeUsernameMinLength
		case field == "Username" && msg == "用户名长度不能超过20个字符":
			fieldErrors[field] = appErrors.ErrCodeUsernameMaxLength
		case field == "Password" && msg == "密码长度不能少于6个字符":
			fieldErrors[field] = appErrors.ErrCodePasswordMinLength
		case field == "Password" && msg == "密码长度不能超过20个字符":
			fieldErrors[field] = appErrors.ErrCodePasswordMaxLength
		case field == "Email" && msg == "邮箱格式不正确":
			fieldErrors[field] = appErrors.ErrCodeEmailFormat
		case field == "Nickname" && msg == "昵称长度不能超过50个字符":
			fieldErrors[field] = appErrors.ErrCodeNicknameMaxLength
		case field == "Avatar" && msg == "头像URL长度不能超过255个字符":
			fieldErrors[field] = appErrors.ErrCodeAvatarMaxLength
		case field == "Role" && msg == "角色不能为空":
			fieldErrors[field] = appErrors.ErrCodeRoleRequired
		case field == "body" && msg == "请求JSON格式不正确，请检查语法":
			return appErrors.JSONFormatError()
		default:
			// 对于未识别的错误，使用通用验证错误
			return appErrors.New(appErrors.ErrCodeValidation, "").WithDetails(map[string]interface{}{
				"fields": fields,
			})
		}
	}

	return appErrors.ValidationErrors(fieldErrors)
}

// DatabaseError 数据库错误
func DatabaseError(err error) appErrors.AppError {
	return appErrors.Wrap(err, appErrors.ErrCodeDatabase, "")
}

// InvalidUsername 用户名无效
func InvalidUsername(username string) appErrors.AppError {
	return appErrors.New(appErrors.ErrCodeValidation, "用户名格式不正确，只能包含字母、数字和下划线，长度在3-20之间").WithDetails(map[string]interface{}{
		"username": username,
	})
}

// InvalidEmail 邮箱无效
func InvalidEmail(email string) appErrors.AppError {
	return appErrors.New(appErrors.ErrCodeEmailFormat, "").WithDetails(map[string]interface{}{
		"email": email,
	})
}

// InvalidPassword 密码无效
func InvalidPassword() errors.AppError {
	return errors.New(errors.ErrCodeValidation, "密码格式不正确，必须包含大小写字母和数字，长度在8-20之间")
}

// InvalidRole 角色无效
func InvalidRole(role string) errors.AppError {
	return errors.New(errors.ErrCodeValidation, "角色无效").WithDetails(map[string]interface{}{
		"role": role,
	})
}

// InvalidStatus 状态无效
func InvalidStatus(status int) errors.AppError {
	return errors.New(errors.ErrCodeInvalidUserStatus, "").WithDetails(map[string]interface{}{
		"status": status,
	})
}

// TokenGenerationFailed 生成令牌失败
func TokenGenerationFailed(err error) errors.AppError {
	return errors.Wrap(err, errors.ErrCodeSystem, "生成令牌失败")
}
